package com.example.wnn.utils;

import org.apache.tomcat.util.buf.HexUtils;

import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmaileUtils {

    /**
     * base64加密
     * @param emaile 待加密内容
     * @return toHexString
     */
    public static String base64Encrypt(final String emaile) {
        byte[] base64Encrypt = Base64.getEncoder().encode(emaile.getBytes());
        return HexUtils.toHexString(base64Encrypt);
    }

    /**
     * base64解密
     * @param toHexString 已加密内容
     * @return String
     */
    public static String base64Decrypt(final String toHexString) {
        byte[] encrypt = HexUtils.fromHexString(toHexString);
        byte[] decrypt = Base64.getDecoder().decode(encrypt);
        return new String(decrypt);
    }


    public static boolean checkEmaile(String emaile){
        String RULE_EMAIL = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
        //正则表达式的模式 编译正则表达式
        Pattern p = Pattern.compile(RULE_EMAIL);
        //正则表达式的匹配器
        Matcher m = p.matcher(emaile);
        //进行正则匹配
        return m.matches();
    }

    /**
     * 验证手机号码
     * @param mobiles
     * @return
     */
    public static boolean isMobileNO(String mobiles){
        boolean flag = false;
        if (mobiles.length() > 11) {
            return flag;
        }
        try{
            String REGEX_MOBILE ="^((13[0-9])|(14(0|[5-7]|9))|(15([0-3]|[5-9]))|(16(2|[5-7]))|(17[0-8])|(18[0-9])|(19([0-3]|[5-9])))\\\\d{8}$";
            return Pattern.matches(REGEX_MOBILE, mobiles);
        }catch(Exception e){
            flag = false;
        }
        return flag;
    }
}
